package com.example.xianji.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.JunMing_Adapter;
import com.example.xianji.BGARefresh.BGARefreshLayout;
import com.example.xianji.BGARefresh.BGARefreshLayout.BGARefreshLayoutDelegate;
import com.example.xianji.bean.JunMingRongHe;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main_JunMing_RongHeActivity extends Activity implements OnClickListener, BGARefreshLayoutDelegate {
    public static String text_junming_content = "";//军民融合搜索关键字
    private ImageView junming_ronghe_search, junming_image_load;
    private RelativeLayout junming_ronghe_back;
    private BGARefreshLayout junming_RefreshLayout;
    private ListView junming_ronghe_listview;
    private JunMing_Adapter JunMing_Adapter;
    private ArrayList<Integer> data;
    private ArrayList<JunMingRongHe> junming_list = new ArrayList<JunMingRongHe>();
    private int page = 1;
    private TextView text_junming_ronghe_title;
    //    private SharedPreferences sp;
//    private Editor ed;
    private AnimationDrawable junming_animation;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                junming_ronghe_listview.setAdapter(JunMing_Adapter);
                junming_RefreshLayout.endRefreshing();
                junming_animation.stop();
                junming_image_load.setVisibility(View.GONE);
            }
            if (msg.what == 2) {
                JunMing_Adapter.notifyDataSetChanged();
                junming_RefreshLayout.endLoadingMore();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__jun_ming__rong_he);
        //XJApplication.getInstance3().addActivity3(this);

        //XJApplication.getInstance().addActivity(this);
        junming_ronghe_back = (RelativeLayout) findViewById(R.id.junming_ronghe_relative_back);
        junming_RefreshLayout = (BGARefreshLayout) findViewById(R.id.junming_RefreshLayout);
        junming_ronghe_listview = (ListView) findViewById(R.id.junming_ronghe_listview);
        junming_ronghe_search = (ImageView) findViewById(R.id.junming_ronghe_search);
        junming_image_load = (ImageView) findViewById(R.id.junming_image_load);
        junming_animation = (AnimationDrawable) junming_image_load.getDrawable();
        junming_animation.start();

        text_junming_ronghe_title = (TextView) findViewById(R.id.text_junming_ronghe_title);
        JunMing_Adapter = new JunMing_Adapter(Main_JunMing_RongHeActivity.this, junming_list);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        if (text_junming_content.length() > 0) {
            text_junming_ronghe_title.setText(text_junming_content);
        } else {
            text_junming_ronghe_title.setText("军民融合");
        }
        XJApplication.cachThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                junming_data();
            }
        });
        junming_ronghe_back.setOnClickListener(this);
        junming_ronghe_search.setOnClickListener(this);
        junming_RefreshLayout.setDelegate(this);
        processLogic(savedInstanceState);
    }

    protected void processLogic(Bundle savedInstanceState) {
        junming_RefreshLayout.setRefreshViewHolder(new com.example.xianji.BGARefresh.BGANormalRefreshViewHolder(this, true));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.junming_ronghe_relative_back) {
            this.finish();
        } else if (v.getId() == R.id.junming_ronghe_search) {
            Intent intent = new Intent(Main_JunMing_RongHeActivity.this, Main_SearchActivity.class);
            intent.putExtra("search", 5);
            startActivity(intent);
        }
    }

    public void junming_data() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("k", text_junming_content);
        parms.addBodyParameter("page", 1 + "");
        httpUtils.send(HttpMethod.POST, Contant.junming_liebiao, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                junming_RefreshLayout.endRefreshing();
                junming_animation.stop();
                junming_image_load.setVisibility(View.GONE);
                Toast.makeText(Main_JunMing_RongHeActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    if (object.getInt("status") == 1) {

                        junming_list.clear();
                        JSONArray array = object.getJSONArray("data");
                        if (array.length() > 0) {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject ob = array.getJSONObject(i);
                                JunMingRongHe mJunMingRongHe = new JunMingRongHe();

                                mJunMingRongHe.setTitle(ob.getString("title"));
                                mJunMingRongHe.setName(ob.getJSONObject("parents").getString("name"));
                                mJunMingRongHe.setId(ob.getInt("id"));
                                junming_list.add(mJunMingRongHe);
                            }
                        } else {
                            Toast.makeText(Main_JunMing_RongHeActivity.this, "暂无数据", Toast.LENGTH_LONG).show();
                        }
                        handler.sendEmptyMessage(1);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void junming_data_more() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("page", page + "");
        parms.addBodyParameter("k", text_junming_content);
        httpUtils.send(HttpMethod.POST, Contant.junming_liebiao, parms, new RequestCallBack<String>() {
            @Override
            public void onFailure(HttpException arg0, String arg1) {
                junming_RefreshLayout.endLoadingMore();
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    if (object.getInt("status") == 1) {
                        JSONArray array = object.getJSONArray("data");
                        if (array.length() > 0) {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject ob = array.getJSONObject(i);
                                JunMingRongHe mJunMingRongHe = new JunMingRongHe();

                                mJunMingRongHe.setTitle(ob.getString("title"));
                                mJunMingRongHe.setName(ob.getJSONObject("parents").getString("name"));
                                mJunMingRongHe.setId(ob.getInt("id"));
                                junming_list.add(mJunMingRongHe);
                            }
                        } else {
                            Toast.makeText(Main_JunMing_RongHeActivity.this, "没有更多数据", Toast.LENGTH_LONG).show();
                        }
                        handler.sendEmptyMessage(2);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (text_junming_content.length() > 0) {
            text_junming_ronghe_title.setText(text_junming_content);
        } else {
            text_junming_ronghe_title.setText("军民融合");
        }
        if (Main_SearchActivity.junming_search_id == 1) {
            junming_data();
            Main_SearchActivity.junming_search_id = 0;
        }
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                page = 1;
                junming_data();
            }
        });
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                page++;
                junming_data_more();
            }
        });

        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        text_junming_content = "";
    }
}
